// pages/management/set-peel/index.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeId: null,
    active: null,
    cropData: [],
    columns: ["一次除皮", "二次除皮"],
    showPicker: false
  },

  onPickerChange(event) {
    const { value, index } = event.detail;
    const { cropData } = this.data;
    cropData[this.data.active]["peelingType"] = index + 1;
    cropData[this.data.active]["peelingName"] = value;
    this.setData({
      cropData,
      showPicker: false
    });
  },

  onCancel() {
    this.setData({
      showPicker: false
    });
  },

  // 保存
  onAdd() {
    if (this.data.cropData.length === 0) {
      return false;
    }
    let params = this.data.cropData[0];
    let url = "addCateGory";
    wx.showLoading({
      title: "获取中..."
    });
    wx.$apis[url]({
      varietyId: params.id,
      varietyName: params.varietyName,
      cropId: params.cropId,
      cropName: params.cropName,
      peelingType: params.peelingType,
      companyId: app.globalData.userInfo.companyData.companyId
    }).then((res) => {
      wx.showToast({
        title: "设置成功",
        duration: 2000
      }).then(() => {
        wx.navigateBack({
          delta: 2
        });
      });
    });
  },

  onSelect(e) {
    let active = e.currentTarget.dataset.index;
    let activeId = e.currentTarget.dataset.id;
    this.setData({
      active,
      activeId,
      showPicker: true
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let cropData = wx.getStorageSync("cropData") || [];
    this.setData({
      cropData: cropData.map((item) => {
        return {
          ...item,
          peelingType: null,
          peelingName: null
        };
      })
    });
    wx.removeStorage({
      key: "cropData"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
